﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DotNetNuke.ComponentModel;
using DotNetNuke.Data;

namespace BrandonHaynes.Data
	{
	public class LinqToEntitiesAdaptedDataProvider : SqlDataProvider
		{
		#region Public Properties

		public override Dictionary<string, string> Settings
			{
			get
				{
				return ComponentFactory.GetComponentSettings<LinqToEntitiesAdaptedDataProvider>() as Dictionary<string, string>;
				}
			}
		#endregion

		#region Updated Methods

		public IDataReader GetTabs(int? PortalId)
			{
			using (var context = new DotNetNuke5())
				return new DataReaders.vw_TabsDataReader(context.vw_Tabs
					.Where(tab => tab.PortalID == PortalId || (tab.PortalID == null && PortalId == null))
					.OrderBy(tab => tab.Level)
					.ThenBy(tab => tab.ParentId)
					.ThenBy(tab => tab.TabOrder));
			}

		public int AddTab(int PortalId, string TabName, bool IsVisible, bool DisableLink, int? ParentId, string IconFile, string Title, string Description, string KeyWords, Uri Url, string SkinSrc, string ContainerSrc, string TabPath, DateTime? StartDate, DateTime? EndDate, int RefreshInterval, string PageHeadText, bool IsSecure)
			{
			using (var context = new DotNetNuke5())
				{
				var tab = new Tabs()
				{
					ContainerSrc = ContainerSrc,
					Description = Description,
					DisableLink = DisableLink,
					EndDate = EndDate,
					IconFile = IconFile,
					IsSecure = IsSecure,
					IsVisible = IsVisible,
					KeyWords = KeyWords,
					PageHeadText = PageHeadText,
					Portals = context.Portals.First(portal => portal.PortalID == PortalId),
					RefreshInterval = RefreshInterval,
					SkinSrc = SkinSrc,
					StartDate = StartDate,
					TabName = TabName,
					TabPath = TabPath,
					Tabs2 = ParentId == null ? null : context.Tabs.First(parentTab => parentTab.TabID == ParentId),
					Title = Title
				};

				context.AddToTabs(tab);
				context.SaveChanges();

				return tab.TabID;
				}
			}

		#endregion

		#region DataProvider Implementation

		#region Pages

		public override IDataReader GetTabs(int PortalId)
			{ return GetTabs(PortalId.ToNullable()); }

		public override int AddTab(int PortalId, string TabName, bool IsVisible, bool DisableLink, int ParentId, string IconFile, string Title, string Description, string KeyWords, string Url, string SkinSrc, string ContainerSrc, string TabPath, DateTime StartDate, DateTime EndDate, int RefreshInterval, string PageHeadText, bool IsSecure)
			{ return AddTab(PortalId, TabName, IsVisible, DisableLink, ParentId.ToNullable(), IconFile, Title, Description, KeyWords, string.IsNullOrEmpty(Url) ? null : new Uri(Url), SkinSrc, ContainerSrc, TabPath, StartDate.ToNullable(), EndDate.ToNullable(), RefreshInterval, PageHeadText, IsSecure); }

		public override void DeleteTab(int TabId)
			{
			using (var context = new DotNetNuke5())
				{
				context.DeleteObject(context.Tabs.First(tab => tab.TabID == TabId));
				context.SaveChanges();
				}
			}

		#endregion

		#region Websites

		public override IDataReader GetPortals()
			{
			using (var context = new DotNetNuke5())
				return new DataReaders.vw_PortalsDataReader(context.vw_Portals);
			}

		public override IDataReader GetPortal(int PortalId)
			{
			using (var context = new DotNetNuke5())
				return new DataReaders.vw_PortalsDataReader(context.vw_Portals
					.First(portal => portal.PortalID == PortalId)
					.ToEnumerable());
			}

		#endregion

		#region Site Log

		public override void AddSiteLog(DateTime DateTime, int PortalId, int UserId, string Referrer, string URL, string UserAgent, string UserHostAddress, string UserHostName, int TabId, int AffiliateId)
			{
			using (var context = new DotNetNuke5())
				{
				context.AddToSiteLog(new SiteLog()
					{
						AffiliateId = AffiliateId,
						DateTime = DateTime,
						Portals = context.Portals.First(portal => portal.PortalID == PortalId),
						Referrer = Referrer,
						TabId = TabId,
						Url = URL,
						UserAgent = UserAgent,
						UserHostAddress = UserHostAddress,
						UserHostName = UserHostName,
						UserId = UserId
					});
				
				context.SaveChanges();
				}
			}

		#endregion

		#endregion
		}
	}
